package leetcode.code0968;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {
	public int minCameraCover(TreeNode root) {
		if (root == null)
			return 0;
		return f(root, false, false);
	}

	// watched 上级节点是否有监控
	public int f(TreeNode root, boolean watched, boolean coverd) {
		if (root == null) {
			return 0;
		}
		if (watched) {// 上级被监控
			int ans1 = 1 + f(root.left, true, true) + f(root.right, true, true);
			int ans2 = minCameraCover(root.left) + minCameraCover(root.right);
			return Math.min(ans1, ans2);
		} else if (coverd) {
			return minCameraCover(root);
		}
		return 1 + f(root.left, true, true) + f(root.right, true, true);
	}

	@Override
	public void debug4() {
		H.compare(1, this.minCameraCover(H.treeNode("[0,0,null,0,0]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		Map<String,String> map = new HashMap<>();
	}

}
